using Microsoft.NetEnterpriseServers;
using Microsoft.SqlServer.Management.SqlMgmt;
using Microsoft.SqlServer.Management.UI;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	public class DatabaseDetailDialog : Form
	{
		private IContainer components;

		private Label lblDatabaseName;

		private TextBox txtDatabaseName;

		private Label lblServerName;

		private TextBox txtServerName;

		private GroupBox grpAuth;

		private CheckBox chbSavePassword;

		private TextBox txtPassword;

		private Label lblPassword;

		private TextBox txtUsername;

		private Label lblUsername;

		private Panel pnlFooterButtons;

		private Button btnOk;

		private Button btnCancel;

		private DatabaseEntity _database;

		private IMessageBoxProvider _messageProvider;

		private bool _isCheckedForChanges;

		private bool _isChanged;

		private WizardPage _parent;

		public WizardPage WizardParent
		{
			get
			{
				return this._parent;
			}
		}

		public DatabaseEntity Database
		{
			get
			{
				return this._database;
			}
			set
			{
				this._database = value;
				this.BindControls();
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.lblDatabaseName = new Label();
			this.txtDatabaseName = new TextBox();
			this.lblServerName = new Label();
			this.txtServerName = new TextBox();
			this.grpAuth = new GroupBox();
			this.chbSavePassword = new CheckBox();
			this.txtPassword = new TextBox();
			this.lblPassword = new Label();
			this.txtUsername = new TextBox();
			this.lblUsername = new Label();
			this.pnlFooterButtons = new Panel();
			this.btnOk = new Button();
			this.btnCancel = new Button();
			this.grpAuth.SuspendLayout();
			this.pnlFooterButtons.SuspendLayout();
			base.SuspendLayout();
			this.lblDatabaseName.AutoSize = true;
			this.lblDatabaseName.Location = new Point(12, 35);
			this.lblDatabaseName.Name = "lblDatabaseName";
			this.lblDatabaseName.Size = new Size(22, 13);
			this.lblDatabaseName.TabIndex = 0;
			this.lblDatabaseName.Text = "OK";
			this.txtDatabaseName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtDatabaseName.Location = new Point(158, 32);
			this.txtDatabaseName.Name = "txtDatabaseName";
			this.txtDatabaseName.Size = new Size(273, 20);
			this.txtDatabaseName.TabIndex = 2;
			this.lblServerName.AutoSize = true;
			this.lblServerName.Location = new Point(12, 9);
			this.lblServerName.Name = "lblServerName";
			this.lblServerName.Size = new Size(22, 13);
			this.lblServerName.TabIndex = 0;
			this.lblServerName.Text = "OK";
			this.txtServerName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtServerName.Location = new Point(158, 6);
			this.txtServerName.MaxLength = 128;
			this.txtServerName.Name = "txtServerName";
			this.txtServerName.Size = new Size(273, 20);
			this.txtServerName.TabIndex = 1;
			this.grpAuth.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.grpAuth.Controls.Add(this.chbSavePassword);
			this.grpAuth.Controls.Add(this.txtPassword);
			this.grpAuth.Controls.Add(this.lblPassword);
			this.grpAuth.Controls.Add(this.txtUsername);
			this.grpAuth.Controls.Add(this.lblUsername);
			this.grpAuth.Location = new Point(15, 58);
			this.grpAuth.Name = "grpAuth";
			this.grpAuth.Size = new Size(416, 96);
			this.grpAuth.TabIndex = 4;
			this.grpAuth.TabStop = false;
			this.grpAuth.Text = "A:OK";
			this.chbSavePassword.AutoSize = true;
			this.chbSavePassword.Location = new Point(31, 73);
			this.chbSavePassword.Name = "chbSavePassword";
			this.chbSavePassword.Size = new Size(41, 17);
			this.chbSavePassword.TabIndex = 7;
			this.chbSavePassword.Text = "A:OK";
			this.chbSavePassword.UseVisualStyleBackColor = true;
			this.chbSavePassword.CheckedChanged += new EventHandler(this.chbSavePassword_CheckedChanged);
			this.txtPassword.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtPassword.Location = new Point(116, 45);
			this.txtPassword.MaxLength = 128;
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new Size(294, 20);
			this.txtPassword.TabIndex = 6;
			this.lblPassword.AutoSize = true;
			this.lblPassword.Location = new Point(14, 48);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.Size = new Size(22, 13);
			this.lblPassword.TabIndex = 0;
			this.lblPassword.Text = "A:OK";
			this.txtUsername.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtUsername.Location = new Point(116, 19);
			this.txtUsername.MaxLength = 128;
			this.txtUsername.Name = "txtUsername";
			this.txtUsername.Size = new Size(294, 20);
			this.txtUsername.TabIndex = 5;
			this.lblUsername.AutoSize = true;
			this.lblUsername.Location = new Point(14, 22);
			this.lblUsername.Name = "lblUsername";
			this.lblUsername.Size = new Size(22, 13);
			this.lblUsername.TabIndex = 0;
			this.lblUsername.Text = "A:OK";
			this.pnlFooterButtons.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.pnlFooterButtons.Controls.Add(this.btnOk);
			this.pnlFooterButtons.Controls.Add(this.btnCancel);
			this.pnlFooterButtons.Location = new Point(1, 157);
			this.pnlFooterButtons.Name = "pnlFooterButtons";
			this.pnlFooterButtons.Size = new Size(438, 29);
			this.pnlFooterButtons.TabIndex = 5;
			this.btnOk.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnOk.Location = new Point(275, 3);
			this.btnOk.Name = "btnOk";
			this.btnOk.Size = new Size(75, 23);
			this.btnOk.TabIndex = 8;
			this.btnOk.Text = "A:OK";
			this.btnOk.UseVisualStyleBackColor = true;
			this.btnOk.Click += new EventHandler(this.btnOk_Click);
			this.btnCancel.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnCancel.DialogResult = DialogResult.Cancel;
			this.btnCancel.Location = new Point(356, 3);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new Size(75, 23);
			this.btnCancel.TabIndex = 9;
			this.btnCancel.Text = "A:OK";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
			base.AcceptButton = this.btnOk;
			base.AutoScaleDimensions = new SizeF(6f, 13f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.CancelButton = this.btnCancel;
			base.ClientSize = new Size(440, 189);
			base.Controls.Add(this.pnlFooterButtons);
			base.Controls.Add(this.grpAuth);
			base.Controls.Add(this.txtServerName);
			base.Controls.Add(this.lblServerName);
			base.Controls.Add(this.txtDatabaseName);
			base.Controls.Add(this.lblDatabaseName);
			this.MinimumSize = new Size(448, 223);
			base.Name = "DatabaseDetailDialog";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterParent;
			this.Text = "A:OK";
			base.FormClosing += new FormClosingEventHandler(this.DatabaseDetailDialog_FormClosing);
			base.Load += new EventHandler(this.DatabaseDetailDialog_Load);
			this.grpAuth.ResumeLayout(false);
			this.grpAuth.PerformLayout();
			this.pnlFooterButtons.ResumeLayout(false);
			base.ResumeLayout(false);
			base.PerformLayout();
		}

		public DatabaseDetailDialog(WizardPage parent, IMessageBoxProvider messageProvider)
		{
			this.InitializeComponent();
			this._messageProvider = messageProvider;
			this._parent = parent;
			base.Icon = CUtils.LoadIcon("server.ico");
		}

		private void BindControls()
		{
			this.txtDatabaseName.Text = this._database.DatabaseName;
			if (this._database.SavePassword)
			{
				this.txtPassword.Text = this._database.Password;
			}
			else
			{
				this.txtPassword.Text = string.Empty;
			}
			this.txtUsername.Text = this._database.UserName;
			this.txtServerName.Text = this._database.ServerName;
			this.chbSavePassword.Checked = this._database.SavePassword;
		}

		private void LoadStrings()
		{
			this.Text = string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.DatabaseDetailTitle, new object[]
			{
				this._database.DatabaseName
			});
			this.lblDatabaseName.Text = GenScriptWizSR.DatabaseDetailDatabaseName;
			this.lblPassword.Text = GenScriptWizSR.DatabaseDetailPassword;
			this.lblUsername.Text = GenScriptWizSR.DatabaseDetailUserName;
			this.lblServerName.Text = GenScriptWizSR.DatabaseDetailServerName;
			this.grpAuth.Text = GenScriptWizSR.DatabaseDetailAuthentication;
			this.chbSavePassword.Text = GenScriptWizSR.DatabaseDetailSavePassword;
			this.btnOk.Text = GenScriptWizSR.OK;
			this.btnCancel.Text = GenScriptWizSR.CancelBtn;
		}

		private bool SaveChanges()
		{
			if (this.ValidateFields())
			{
				this._database.DatabaseName = this.txtDatabaseName.Text;
				this._database.UserName = this.txtUsername.Text;
				this._database.ServerName = this.txtServerName.Text;
				this._database.Password = this.txtPassword.Text;
				this._database.SavePassword = this.chbSavePassword.Checked;
				this._isChanged = false;
				return true;
			}
			return false;
		}

		private void AbortChanges()
		{
			this._isChanged = false;
		}

		private void CloseDialog()
		{
			base.Close();
		}

		private bool ValidateFields()
		{
			bool result = true;
			string text = null;
			if (string.IsNullOrEmpty(this.txtDatabaseName.Text))
			{
				text = GenScriptWizSR.DatabaseDetailDatabaseName;
			}
			else if (string.IsNullOrEmpty(this.txtServerName.Text))
			{
				text = GenScriptWizSR.DatabaseDetailServerName;
			}
			else if (string.IsNullOrEmpty(this.txtUsername.Text))
			{
				text = GenScriptWizSR.DatabaseDetailUserName;
			}
			if (!string.IsNullOrEmpty(text))
			{
				this._messageProvider.ShowMessage(string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.DatabaseDetailEmptyDatabaseNameMessage, new object[]
				{
					text
				}), GenScriptWizSR.DatabaseDetailEmptyDatabaseNameMessageTitle, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this.WizardParent);
				result = false;
			}
			return result;
		}

		private void chbSavePassword_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbSavePassword.Checked)
			{
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.SaveChanges())
			{
				this.CloseDialog();
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this._isCheckedForChanges = true;
			if (this.CheckChangesForSaving())
			{
				this.AbortChanges();
				this.CloseDialog();
			}
		}

		private void DatabaseDetailDialog_Load(object sender, EventArgs e)
		{
			this._isChanged = false;
			this.txtServerName.Select();
			this.LoadStrings();
			this.txtPassword.TextChanged += new EventHandler(this.DatabaseFieldsTextChanged);
			this.txtUsername.TextChanged += new EventHandler(this.DatabaseFieldsTextChanged);
			this.txtDatabaseName.TextChanged += new EventHandler(this.DatabaseFieldsTextChanged);
			this.txtServerName.TextChanged += new EventHandler(this.DatabaseFieldsTextChanged);
		}

		private void DatabaseFieldsTextChanged(object sender, EventArgs e)
		{
			if (!this._isChanged)
			{
				this._isChanged = true;
			}
		}

		private void DatabaseDetailDialog_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!this._isCheckedForChanges)
			{
				if (!this.CheckChangesForSaving())
				{
					e.Cancel = true;
				}
			}
			else if (this._isChanged)
			{
				e.Cancel = true;
			}
			this._isCheckedForChanges = false;
		}

		private bool CheckChangesForSaving()
		{
			bool result = false;
			if (this._isChanged)
			{
				DialogResult dialogResult = this._messageProvider.ShowMessage(string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.DatabaseDetailSaveChangeMessage, new object[]
				{
					this._database.DatabaseName
				}), GenScriptWizSR.DatabaseDetailSaveChangeTitle, ExceptionMessageBoxButtons.YesNoCancel, ExceptionMessageBoxSymbol.Question, this.WizardParent);
				if (dialogResult == DialogResult.Yes)
				{
					result = this.SaveChanges();
				}
				else if (dialogResult == DialogResult.No)
				{
					this.AbortChanges();
					result = true;
				}
				else if (dialogResult == DialogResult.Cancel)
				{
					result = false;
				}
			}
			else
			{
				result = true;
			}
			return result;
		}
	}
}
